#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CFNetwork/CFNetwork.h>
#import <UIKit/UIKit.h>
#import "ContactListViewController.h"
#import "CalendarListViewController.h"
#import "ContactContentViewController.h"
#import "Person.h"
#import "Calendar.h"
#import "MailListViewController.h"
#import "MailViewController.h"
#import "Mail.h"
#import "MailContentViewController.h"
#import "AuthenticationController.h"
#import "DatabaseInteraction.h"
#import "MailEditViewController.h"
#import "TransactionViewController.h";

@interface IDominoAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate, UIWebViewDelegate,UIAlertViewDelegate> {
//The main Window
    UIWindow *window;
    UITabBarController *tabBarController;
//All Controllers
	UINavigationController* mailNavigationController;
	UINavigationController *contactNavigationController;
	UINavigationController *calendarNavigationController;
	
	UINavigationController* currentNavigationController;// not retained, just point to the current NavigationController for convenience
	
	AuthenticationController *authenticationController;//If the Username and password or any server configuration is wrong, use this controller.
	
	MailViewController* mailBoxViewController;//Currently it is only Inbox, Drafts, Sent And All. Trash use a different logic.
	MailListViewController *mailListViewController;
	MailContentViewController	*mailContentController;
	MailEditViewController	*mailEditController;
	UIViewController	*mailWebViewController;
	
	ContactListViewController *contactListViewController;
	ContactContentViewController *contactContentController;
	UIViewController *contactContentWebViewController;

	CalendarListViewController *calendarListViewController;
	UIViewController *calendarContentController;
	
	TransactionViewController* transactionViewController;
	
//Login Info
	NSString* _loginUsername;
	NSString* _loginPassword;
	NSString* _mailServer;
	NSString* _databaseName;
	NSString* _serverName;
	NSURL* _serverLoginURL;
	NSURL* _serverGetCookieURL;
	NSMutableString* _sessionCookie;
	
//WebView
	UIWebView* mailWebView;		
	UIWebView* calendarWebView;//***************************not using web view is a good idea
	UIWebView* contactContentWebView;
//segment Control for mail and calendar content View
	UISegmentedControl *segmentedControl;

//The Genral Mail Variables, maybe can move into MailViewController
	Mail* currentMail;
	NSString* currentMailBox;
	NSMutableDictionary* mailBoxDict;
	NSMutableDictionary* localMailDict;
	
//in preference
	int _defaultMaxMail;//default mail read count  in preference
	BOOL _isOffline;//if YES, do not use the network
	BOOL _useWebSeal;
//Database for contacts and tasks
	DatabaseInteraction* databaseInteraction;
	
//Queue for handle offline request
	NSOperationQueue *requestQueue;
//Thread safety
	NSLock *theLock;
	

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) UINavigationController *mailNavigationController;
@property (nonatomic, retain) MailListViewController *mailListViewController;
@property (nonatomic, retain) MailViewController *mailBoxViewController;
@property (nonatomic, retain) MailContentViewController *mailContentController;
@property (nonatomic, retain) MailEditViewController *mailEditController;
@property (nonatomic, retain) TransactionViewController *transactionViewController;
@property (nonatomic, retain) UIViewController *mailWebViewController;
@property (nonatomic, retain) UIWebView *mailWebView;
@property (nonatomic, retain) UIWebView *calendarWebView;
@property (nonatomic, retain) UIWebView *contactContentWebView;
@property (nonatomic, retain) NSString *currentMailBox;
@property (nonatomic, retain) NSMutableDictionary *mailBoxDict;
@property (nonatomic, retain) NSMutableDictionary *localMailDict;

@property (nonatomic, retain) UINavigationController *contactNavigationController;
@property (nonatomic, retain) ContactListViewController *contactListViewController;
@property (nonatomic, retain) ContactContentViewController *contactContentController;

@property (nonatomic, retain) UINavigationController *calendarNavigationController;
@property (nonatomic, retain) UITableViewController *calendarListViewController;
@property (nonatomic, retain) UIViewController *calendarContentController;
@property (nonatomic, retain) UIViewController *contactContentWebViewController;
@property (nonatomic, retain) UIViewController *authenticationController;

@property (nonatomic, retain) UISegmentedControl *segmentedControl;//***************************trouble

@property (nonatomic, retain) NSString* _loginUsername;
@property (nonatomic, retain) NSString* _loginPassword;
@property (nonatomic, retain) NSString* _mailServer;
@property (nonatomic, retain) NSString* _databaseName;
@property (nonatomic, retain) NSString* _sessionCookie;
@property (nonatomic, retain) NSString* _serverName;
@property (nonatomic, retain) NSURL* _serverLoginURL;
@property (nonatomic, retain) NSURL* _serverGetCookieURL;
@property (nonatomic, retain) DatabaseInteraction *databaseInteraction;
@property (nonatomic, retain) NSOperationQueue *requestQueue;


//Utilitiy Function

- (void)login;
- (void)getCalendarList;
- (void)showPersonInfo:(Person *)personInfo;
- (void)showDirectoryPersonInfo:(Person *)personInfo;
- (void)showCalendarInfo:(Calendar *)calendarInfo;
- (void)getMailList;
- (void)initUI;
- (int)defaultMaxMailCount;
//UI
- (void)loadPreference;
- (void)authenInfoAction:(id)sender;
- (void)segmentAction:(id)sender;
- (void)configSegment:(int) SegmentIndex;
- (BOOL) writeApplicationData:(NSData *)data toFile:(NSString *)fileName;
- (NSData *)applicationDataFromFile:(NSString *)fileName;
- (void)readMoreMails;
- (void)loadLocalMails:(NSString*) mailBox;
- (void)refreshSent;
- (void)refreshAction:(id)sender;
- (BOOL)isDataSourceAvailable;
- (void)showMailInfo:(Mail *)mailInfo;
- (void)updateContacts:(BOOL)isPersonal;
- (void)selectEmailFromDatabase;
- (void)getPersonInfoByPerson:(Person*)personInfo;
- (void)getDirectoryInfoByPerson:(Person*)personInfo;
-(void)returnSelectEmail:(NSString*)mail;
- (void)cancelEmailSelection:(id)sender;
-(int)totalMailCount;
-(NSString *)sendHttpRequest:(NSURL*)url isPost:(BOOL)isPostMethod postData:(NSData*)data;
-(void) savePreloadingMails:(NSArray*)mailArray mailBox:(NSString*)mailBox;
- (void)getSavedTransaction;
- (void)sendMailTo:(NSString*) receiver;
-(void)showNotLoginAlert;
- (void)setMailReadStatusWithUNID:(NSString *)mailUNID markRead:(BOOL)status ;
- (void) loginDirect;
@end
